#!/usr/bin/python

import sys as s
import pygame as p
from Load import Load
from Screens import *
from Saves import *
from war import *

p.init()
print("The game is loading...")
screen=p.display.set_mode(size=(854,480),flags=p.RESIZABLE)
data=Load()
p.display.set_icon(data.icon)
p.display.set_caption('学校大作战 骨灰级破解版')
print("The game is loaded. Enjoy the School Battle!^_^")
print("Now you are in screen1. Loading screen1...")
# background=p.transform.scale(data.screen1,(854,480))
location=1
buttons=show_screen1(data=data,screen=screen)
print("screen1 is loaded")
filename:str=None
show_func=show_school_map
size=(854,480)
rotateddd=False

while True:
    if location==1:
        buttons=show_screen1(data=data,screen=screen,w=size[0],h=size[1],rotatedd=rotateddd)
    for e in p.event.get():
        if e.type==p.KEYDOWN:
            print("you push the keyboard, key is",e.key,"mod is",e.mod)
            if e.key==p.K_s and e.mod==p.KMOD_CTRL and location!=1:
                save_game(filename=filename,game=data.vanilla)
        if e.type==p.MOUSEBUTTONDOWN:
            print("you click the screen, x is",e.pos[0],'y is',e.pos[1])
            if e.button==1:
                if location==3 or location==4:
                    close()
                    openedd=False
                    location=3
                    for j in range(12):
                        i:p.Rect
                        i=buttons[j]
#                        print(i.center)
                        if(i.collidepoint(e.pos)):
#                            data.vanilla['v']
                            print("you open ",data.vanilla['locates'][j]['name'],'and number is',j)
                            location=4
                            openedd=True
                            show_func=open(data=data,screen=screen,zy=(data.vanilla['player']['copy']==39),number=j,w=size[0],h=size[1])
                    if not openedd:
                        show_school_map(data=data,screen=screen,w=size[0],h=size[1])
                if location==2:
                    if buttons[0].collidepoint(e.pos):
                        data.vanilla['player']['copy']=40
                    if buttons[1].collidepoint(e.pos):
                        data.vanilla['player']['copy']=39
                    location=3
                    buttons=list()
                    buttonss=show_school_map(data=data,screen=screen,w=size[0],h=size[1])
                    for i in buttonss:
                        cache=data.blue_flag.get_rect()
                        cache.center=i
                        buttons.append(cache)
                if location==1:
                    if buttons[2].collidepoint(e.pos):
                        rotateddd=not rotateddd
                    if buttons[1].collidepoint(e.pos):
                        filename=new_saves()
                    if buttons[0].collidepoint(e.pos):
                        cache=open_saves()
                        if cache!=None:
                            filename=cache[0]
                            data.vanilla=cache[1]
                    if filename!=None:
                        if data.vanilla["player"]['copy']==-1:
                            location=2
                            buttons=show_choosing_zhengying(data=data,screen=screen,w=size[0],h=size[1])
                        else:
                            location=3
                            target=show_school_map(data=data,screen=screen,w=size[0],h=size[1])
                            buttons=list()
                            for i in target:
                                cache=data.blue_flag.get_rect()
                                cache.center=i
                                buttons.append(cache)
        if e.type==p.VIDEORESIZE:
            print("you resize the window, the height is",e.h,"the width is",e.w)
            size=e.size
            if location==4:
                target=show_func(data=data,screen=screen,w=size[0],h=size[1])
            if location==3:
                target=show_school_map(data=data,screen=screen,w=size[0],h=size[1])
                buttons=list()
                for i in target:
                    cache=data.blue_flag.get_rect()
                    cache.center=i
                    buttons.append(cache)
                print(buttons)
            if location==2:
                buttons=show_choosing_zhengying(data=data,screen=screen,w=e.w,h=e.h)
            if location==1:
                buttons=show_screen1(data=data,screen=screen,w=e.w,h=e.h,rotatedd=rotateddd)
        if e.type==p.QUIT:
            if location!=1:save_game(filename=filename,game=data.vanilla)
            print("goodbye!")
            p.quit()
            s.exit()
#    screen.blit(background,(0,0))
    p.display.update()